package com.example.demo.Controller;

import com.example.demo.pojo.Message;
import com.example.demo.pojo.Meta;
import com.example.demo.pojo.mapper.MessageMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SendMessageController {

    @Autowired
    private MessageMapper messageMapper;

    @CrossOrigin
    @GetMapping("/selectMessage")
    public List<Message> seclectMessage() {
        List<Message> msg=messageMapper.seclectMessage();
        for(Message m:msg) {
            System.out.println(m);
        }

        return msg;
    }

    @CrossOrigin
    @RequestMapping(value = "/sendMessage",produces = "text/html;charset=UTF-8")
    public String sendMessage(String msg) throws JsonProcessingException {
        ObjectMapper mapper=new ObjectMapper();
        // 将json字符串转成java对象
        Message message = mapper.readValue(msg,Message.class);
        System.out.println(messageMapper.sendMessage(message));
        return "留言成功";
    }
}
